package org.apogee.mil.client.widgets;

import java.util.ArrayList;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.utils.ServerConfig;
import org.apogee.mil.client.utils.Session;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

public class ServerInfo implements IsWidget {
	
	private ColumnModel cm;
	private Grid<BaseModel> grid;
	private ListStore<BaseModel> store = new ListStore<BaseModel>();
	private int sessionId;
	
	public ServerInfo() {
		this.sessionId = Session.getSessionId();
	}

	@Override
	public Widget asWidget() {
		final ContentPanel cp = new ContentPanel();
		cp.setHeaderVisible(true);
		cp.setBorders(false);
		cp.setHeading("Website Information");
		cp.setWidth(520);
		
		ServerServiceFactory.getGreetService(sessionId).getServerInfo(new AsyncCallback<ArrayList<BaseModel>>() {
			
			@Override
			public void onSuccess(ArrayList<BaseModel> result) {
				if (result.size() == 0) {
					BaseModel bm = new BaseModel();
					bm.set("heading", "Error");
					bm.set("val", "Unable to get information");
					result.add(bm);
				}
				store.add(result);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
			}
		});
		
		
		ServerServiceFactory.getGreetService(sessionId).getServerConfig(new AsyncCallback<ArrayList<BaseModel>>() {
			
			@Override
			public void onSuccess(ArrayList<BaseModel> result) {
				ServerConfig.loadConfig(result);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
			}
		});
		
		GridCellRenderer<BaseModel> renderer = new GridCellRenderer<BaseModel>() {

			@Override
			public Object render(BaseModel model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<BaseModel> store,
					Grid<BaseModel> grid) {

				String val = (String) (model.get(property) == null ? "No Info..." : model.get(property));
				return "<span qtitle='" + cm.getColumnById(property).getHeader() + "' qtip='" + val  
						+ "' style='font-weight: bold;color: #5d8aa8;white-space:normal; height:30px;'>" + val + "</span>";
			}
		};

		ArrayList<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId("heading");
		column.setHeader("Info");
		column.setWidth(100);
		column.setRowHeader(true);
		column.setRenderer(renderer);
		configs.add(column);
		
		column = new ColumnConfig();
		column.setId("val");
		column.setHeader("Version");
		column.setWidth(410);
		column.setRowHeader(true);
		column.setRenderer(renderer);
		configs.add(column);

		cm = new ColumnModel(configs);

		grid = new Grid<BaseModel>(store, cm);
		grid.setBorders(true);
		grid.setColumnLines(true);
		grid.getAriaSupport().setLabelledBy(cp.getHeader().getId()+"-label");
		grid.setAutoHeight(true);
		grid.setWidth(520);
		cp.add(grid);
		
		return cp;
	}

}
